package cn.nju.zyy.action.person;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.nju.zyy.model.person.Person;
import cn.nju.zyy.model.person.State;
import cn.nju.zyy.service.person.PersonService;

import com.opensymphony.xwork2.ActionSupport;

@Component
@Results({
	@Result(name="success", location="person-list.jsp"),
	@Result(name="input", location="person-input.jsp")
})
public class PersonAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private Person person;
	private int id;
	private String[] sports = { "football", "baseball", "basketball" };
	private String[] genders = { "male", "female", "not sure" };
	private List<State> states;
	private String[] carModelsAvailable = { "Ford", "Chrysler", "Toyota",
			"Nissan" };

	private PersonService personService;
	private List<Person> personList = PersonService.getPersonList();

	@Action("person-list")
	public String execute() throws Exception {
		return SUCCESS;
	}

	@Action("create")
	public String create() {
		return INPUT;
	}

	@Action("edit-person")
	public String edit() {
		person = personService.getPerson(id);
		return INPUT;
	}

	@Action("save-or-update")
	public String saveOrUpdate() {
		if (person.getId() > -1) {
			personService.update(person);
		} else {
			personService.save(person);
		}

		personList = PersonService.getPersonList();
		return SUCCESS;
	}

	@Action("delete")
	public String delete() {
		personService.delete(id);
		personList = PersonService.getPersonList();
		return SUCCESS;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String[] getSports() {
		return sports;
	}

	public String[] getGenders() {
		return genders;
	}

	public List<State> getStates() {
		states = new ArrayList<State>();
		states.add(new State("AZ", "Arizona"));
		states.add(new State("CA", "California"));
		states.add(new State("FL", "Florida"));
		states.add(new State("KS", "Kansas"));
		states.add(new State("NY", "New York"));
		return states;
	}

	public String[] getCarModelsAvailable() {
		return carModelsAvailable;
	}

	public List<Person> getPersonList() {
		return personList;
	}

	public void setPersonList(List<Person> personList) {
		this.personList = personList;
	}

	@Autowired
	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

}
